package com.sparrow.source.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sparrow.source.common.dto.AccountQueryDTO;
import com.sparrow.source.common.dto.AccountUpdateDTO;
import com.sparrow.source.common.dto.LoginDTO;
import com.sparrow.source.common.dto.PassWordUpdate;
import com.sparrow.source.common.dto.RegistryDTO;
import com.sparrow.source.common.dto.Result;
import com.sparrow.source.persistense.IasAccount;
import com.sparrow.source.service.IasAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author bryceyung
 * @since 2024/12/5
 */
@RequestMapping("/v1/account")
@Controller
public class AccountController {

    @Autowired
    private IasAccountService accountService;

    @PostMapping("registry")
    @ResponseBody
    public Result registry(@RequestBody RegistryDTO registryDTO, HttpServletRequest request) {
        return accountService.registry(registryDTO, request);
    }

    @PostMapping("login")
    @ResponseBody
    public Result login(@RequestBody LoginDTO loginDTO) {
        return accountService.login(loginDTO.getUserName(), loginDTO.getPassword());
    }

    @PostMapping("passwordUpdate")
    @ResponseBody
    public Result passwordUpdate(@RequestBody PassWordUpdate passWordUpdate, HttpServletRequest request) {
        return accountService.passwordUpdate(passWordUpdate, request);
    }

    @PostMapping("logout")
    @ResponseBody
    public Result logout(@RequestParam("accountId") Integer accountId, HttpServletRequest request) {
        return accountService.logout(accountId, request);
    }

    @PostMapping("checkUserName")
    @ResponseBody
    public Result checkUserName(@RequestParam("userName") String userName) {
        return accountService.checkUserName(userName);
    }

    @PostMapping("query")
    @ResponseBody
    public Result queryAccounts(@RequestBody AccountQueryDTO queryDTO, Page<IasAccount> accountPage) {
        return accountService.queryAccount(accountPage, queryDTO);
    }

    @PostMapping("delete")
    @ResponseBody
    public Result deleteAccount(@RequestParam("accountId") Integer accountId) {
        return accountService.deleteAccount(accountId);
    }

    @PostMapping("update")
    @ResponseBody
    public Result update(@RequestBody AccountUpdateDTO accountUpdateDTO) {
        return accountService.updateStatusOrType(accountUpdateDTO.getAccountId(), accountUpdateDTO.getStatus(), accountUpdateDTO.getType());
    }
}
